package io.jft.doll.ui.bills

import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.g.base.api.ErrorCode
import com.g.base.extend.observeEx
import com.g.base.extend.resultNotNull
import com.g.base.room.entity.UserEntity
import com.g.base.room.repository.Status
import com.g.base.router.RouteExtras
import com.g.base.ui.BaseActivity
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems
import io.jft.doll.R
import io.jft.doll.databinding.ActivittyBillsBinding
import io.jft.doll.model.viewModel.UserViewModel
import io.jft.doll.router.RouterPage
import io.jft.doll.ui.bills.fragment.BillsListFragment

/**
 * Created by G on 2017/11/23 0023.
 */

@Route(path = RouterPage.BillsActivity, extras = RouteExtras.NeedOauth)
class BillsActivity : BaseActivity<ActivittyBillsBinding>() {
    override var hasToolbar: Boolean = true
    private val userViewModel by lazy { ViewModelProviders.of(this).get(UserViewModel::class.java) }

    private val smartTab by lazy { contentView.tabViewPage!!.smartTabLayout!!.smartTabLayout }
    private val viewpager by lazy { contentView.tabViewPage!!.viewPagerLayout!!.viewPager }

    @JvmField
    @Autowired
    var type: String = "-1"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activitty_bills)

        toolbar.title = "我的账单"
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_toolbar_24dp)
        toolbar.setNavigationOnClickListener { finishAfterTransition() }

        viewpager.adapter = FragmentPagerItemAdapter(supportFragmentManager,
                FragmentPagerItems(this).apply {
                    add(FragmentPagerItem.of("全部", BillsListFragment::class.java, Bundle().apply { putString("type", "0") }))
                    add(FragmentPagerItem.of("充值", BillsListFragment::class.java, Bundle().apply { putString("type", "1") }))
                })
        smartTab.setViewPager(viewpager)

        obsData()
        userViewModel.operateRemain()
    }

    private fun obsData() {
        userViewModel.obsRemainingSum()
                .resultNotNull()
                .observeEx(this) {
                    when (it.status) {
                        Status.Loading -> {
                            showLoading()
                        }
                        Status.Content -> {
                            contentView.money.text = it.data!!.toString()
                            showContentView()
                        }
                        Status.Error -> {
                            if (it.error?.code == ErrorCode.EMPTY)
                                showEmpty()
                            else
                                showError(it.error?.message)
                        }
                    }
                }
    }

    override fun onReload() {
        userViewModel.operateRemain()
    }

    override fun onTokenChange(data: UserEntity?) {
        if (data == null) {
            showNeedOauth()
        } else {
            userViewModel.operateRemain()
        }
    }
}